package filesystem;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;

public class Directory extends FSElement
{
	private ArrayList<FSElement> children = new ArrayList<FSElement>();
	
	public Directory(String name, String owner)
	{		
		setName(name);
		setOwner(owner);
	}

	public Directory(String name, 
					 String owner, 
				 	 Directory parent,
				 	 Date created)
	{
		setName(name);
		setOwner(owner);
		setParent(parent);
		setCreated(created);
		setLastModified(created);
		setSizeCalculator(new DirectorySizeCalculator());
	}
	
	public void addChild(FSElement fselement)
	{			
		getChildren().add(fselement);
	}

	public void addChild(FSElement fselement, int index)
	{
		getChildren().add(index, fselement);
	}
	
	public void setChildren(ArrayList<FSElement> children) 
	{
		this.children = children;
	}
	
	public ArrayList<FSElement> getChildren() 
	{
		return children;
	}	
	
	public String getInfo(String prefix)
	{
		
		String result = "";
		result = prefix +  "Directory: " + getName() + "\tOwner: " + 
			getOwner() + "\tSize: " + getSize() + "\tCreated: " +getCreated().toString() + 
			"\tModified: " + getLastModified().toString() + "\n";			
		
		return result;
	}
	
	public String getDetailListing(String prefix)
	{
		String result = "";
		
		result = getInfo(prefix);
		Iterator<FSElement> it = children.iterator();
		while(it.hasNext())
		{
			result += it.next().getInfo(prefix + "\t");
		}	
		
		return result;
	}
	
	public String getListing(String prefix)
	{
		String result = "";
		
		result = getName() + " Directory Listing \n";
		Iterator<FSElement> it = children.iterator();
		while(it.hasNext())
		{
			result += prefix + "\t" +  it.next().getName() + "\n";
		}	
		
		return result;
	}
	
	public boolean isLeaf()
	{
		return false;
	}	
	
	public String[][] getChildrenInfo()
	{
		String result[][] = new String[getChildren().size()][5];		
		
		for (int i= 0; i<getChildren().size(); i++)
		{
			result[i][0] =  getChildren().get(i).getName();
			result[i][1] =	getChildren().get(i) instanceof Directory ? "Folder" : "File";
			result[i][2] =	getChildren().get(i).getSize() + "";
			result[i][3] =	getChildren().get(i).getOwner(); 			
			result[i][4] =	getChildren().get(i).getCreated() + "";
		}
		
		return result;
	}
	
}
